//
//  KeChengXiangQingVC.m
//  ZhiBoYuan
//
//  Created by apple on 2018/4/17.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "KeChengXiangQingVC.h"

#import "KeChengXiangQingCell.h"
#import "CourseInfo.h"
#import "ToolHeader.h"

static NSString *ident = @"KeChengXiangQingCell";

@interface KeChengXiangQingVC ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) NSMutableArray *dataArray;
//@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, assign) NSInteger pageIndex;


@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong)NSDictionary *teacherInfo;

@property (nonatomic, assign)BOOL canScroll;


@property(nonatomic,assign)NSInteger rech;

@end

@implementation KeChengXiangQingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setupConfigureView];
    [self downloadCourseXiangQingDatas];
    _rech = 0;
   
//    __weak __typeof(&*self)weakSelf = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:TABLETOP object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(acceptMsg:) name:TABLELEAVETOP object:nil];
    
    [self getnote];
    
    
}
- (void)acceptMsg:(NSNotification *)notification
{
    NSString *notificationName = notification.name;
    if ([notificationName isEqualToString:TABLETOP])
    {
        NSDictionary *userInfo = notification.userInfo;
        NSString *can = userInfo[NotificationCanScroll];
        if ([can isEqualToString:@"1"])
        {
            self.canScroll = YES;
            self.tableView.showsVerticalScrollIndicator = YES;
        }
        
    }
    else if ([notificationName isEqualToString:TABLELEAVETOP])
    {
        self.canScroll = NO;
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.contentOffset = CGPointZero;
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (!self.canScroll)
    {
        [scrollView setContentOffset:CGPointZero];
    }
    CGFloat offSetY = scrollView.contentOffset.y;
    
    if (offSetY < 0)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:TABLELEAVETOP object:nil userInfo:@{NotificationCanScroll: @"1"}];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)downloadCourseXiangQingDatas{
    SGParameter *parameter  = [[SGParameter alloc] init];
    parameter.body = @{
                       @"course_id":self.course_id// 1 直播 2微课 3套餐
                       };
    [SGHttpTool POST:Host_Append(CourseDetail) parameter:parameter sucess:^(id responseObject) {
        //成功回调
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:nil];
        NSString *resultStr =[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
        ZYLog(@"%@",resultStr);
        SGReceive *receive = [SGReceive receiveWithResponseObject:responseObject dataWord:@"course_info"dataClass:[CourseInfo class]];
        
        _teacherInfo = responseObject[@"data"][@"teacher_info"];
        
        [self.dataArray addObject:receive.data];
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
        
    }];
    
    
}


- (void)setupConfigureView {
    
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate        = self;
    self.tableView.dataSource      = self;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    self.tableView.estimatedRowHeight = 44.0f;//推测高度，必须有，可以随便写多少
    
    self.tableView.rowHeight = UITableViewAutomaticDimension;//iOS8之后默认就是这个值，可以省略
    
    [self.tableView registerNib:[UINib nibWithNibName:ident bundle:nil] forCellReuseIdentifier:ident];
    
    [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];

}


#pragma mark -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    KeChengXiangQingCell *cell = [tableView dequeueReusableCellWithIdentifier:ident forIndexPath:indexPath];
    
        
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.row];
    cell.detailLable.text = [_teacherInfo[@"centify_info"] valueForKey:@"teacher_detail"];
    cell.courseNum.text = [NSString stringWithFormat:@"课程数:%@      |      关注数:%@",_teacherInfo[@"total_course"],_teacherInfo[@"fans_num"]];
    cell.teacherName.text = _teacherInfo[@"name"];
     [cell.headImage sd_setImageWithURL:[NSURL URLWithString:_teacherInfo[@"small_img"]] placeholderImage:[UIImage imageNamed:@""]];
    return cell;
}

#pragma mark ----------------------- 更新高度通知 ------------------------
-(void)getnote{
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:) name:rechNotifyName object:nil];
}

- (void)tongzhi:(NSNotification *)text{
    
    [_tableView reloadData];

}


// 第一界面移除监听的事件
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:rechNotifyName  object:nil];
}



//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    return SNRealValue(50);
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//    AdetailViewController *vc= [[AdetailViewController alloc]initWithNibName:@"AdetailViewController" bundle:nil];
//    
//    vc.hidesBottomBarWhenPushed =YES;
//
//    [self.navigationController pushViewController:vc animated:YES];
}

#pragma makr -- property
- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
